﻿using NativeWifi;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Net.NetworkInformation;
using System.Net.Sockets;
namespace Get_LuYouMac
{
    internal class GetLuYouMac
    {
        public static WlanClient wlanClient = new WlanClient();
        static public bool isVialdNetCardName(string name)
        {
            name = name.ToLower();
            if (name.Contains("ppoe")) return false;
            if (name.Contains("microsoft")) return false;
            if (name.Contains("virtual")) return false;
            if (name.Contains("bluetooth")) return false;
            if (name.Contains("vmnet")) return false;
            if (name.Contains("bthpan")) return false;
            if (name.Contains("vpn")) return false;
            if (name.Contains("ndisip")) return false;
            if (name.Contains("sinforvnic")) return false;
            return true;
        }


        public static string GetRouterMac()
        {
            List<String> mac_list = new List<string>();
            mac_list.Add("58-69-6c-5f-b2-32");
            Process cmd = new Process();
            cmd.StartInfo.FileName = "arp.exe";
            cmd.StartInfo.Arguments = "-a";
            cmd.StartInfo.RedirectStandardOutput = true;
            cmd.StartInfo.RedirectStandardInput = true;
            cmd.StartInfo.UseShellExecute = false;
            cmd.StartInfo.CreateNoWindow = true;
            cmd.Start();
            string info = cmd.StandardOutput.ReadToEnd();
            cmd.WaitForExit();
            cmd.Close();
            string mac = "00-00-00-00-00";
            string[] ips = info.Split(new string[]
            {
                "\r\n"
            }, StringSplitOptions.RemoveEmptyEntries);
            if (ips.Length >= 2)
            {
                for (int i = 0; i < ips.Length; i++)
                {
                    string[] subs = ips[i].Split(new char[]
                    {
                        ' '
                    }, StringSplitOptions.RemoveEmptyEntries);
                    if (mac_list.Contains(subs[1]))
                    {
                        mac = subs[1];
                        break;
                    }
                }
            }
            return mac;
        }

        public static string GetMac()
        {
            GC.Collect();
            string routerMac = null;
            foreach (WlanClient.WlanInterface wlanInterface in wlanClient.Interfaces)
            {

                Wlan.WlanBssEntry[] wlanBssEntries = wlanInterface.GetNetworkBssList();
                foreach (Wlan.WlanBssEntry wlanBssEntry in wlanBssEntries)
                {
                    byte[] macAddr = wlanBssEntry.dot11Bssid;
                    var macAddrLen = (uint)macAddr.Length;
                    var str = new string[(int)macAddrLen];
                    for (int i = 0; i < macAddrLen; i++)
                    {
                        if (i != macAddrLen - 1)
                        {
                            str[i] = macAddr[i].ToString("x2") + ":";
                        }
                        else if (i == macAddrLen - 1)
                        {
                            str[i] = macAddr[i].ToString("x2");
                        }
                    }
                    string mac = string.Join("", str);
                    if (wlanInterface.InterfaceState == Wlan.WlanInterfaceState.Connected && wlanInterface.CurrentConnection.isState == Wlan.WlanInterfaceState.Connected)
                    {
                        routerMac = mac;
                        LabTimmer.Core.WriteLogs.WriteErroTOLogs("导生大大发现一个惊悚的秘密:你的路由mac是 " + mac);
                        return routerMac;
                    }

                }

            }

            return "00:00:00:00:00";
        }

    }
}
